//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    page:1,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),

    menu: [],
    index:0,
    activeIndex:0,
    list: []
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  
  onLoad: function () {
    var that=this
    app.route(this)
    var avatarUrl=wx.getStorageSync('avatarUrl')
		var url = wx.getStorageSync('siteUrl')
    that.setData({
      siteUrl:url
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

    this.dbfenlei()
    this.tuijian()
  },
  once:function(e){
    var n=0;
    var that=this
    that.setData({
      activeIndex:n
    })
    that.tuijian()
  },
  tabClick: function (e) {
    this.setData({
      activeIndex: e.currentTarget.dataset.index+1,
      c_id: e.currentTarget.dataset.id
    })
    this.fl()
  },

  // tabbar.js
  tabbar: function (e) {
    var that = this
    var url = e.currentTarget.dataset.url
    wx.reLaunch({
      url: url,
    })
  },
  


  // 分类获取
  dbfenlei:function(e){
    var that=this
    app.util.request({
      url:'entry/wxapp/catestop',
      success:function(res){
        
        var menu=res.data.res
        that.setData({
          menu: menu
        })
      }
    })
  },

  // 抖宝推荐数据
  tuijian: function (e) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/dbupfee',
      success: function (res){
        if(res.data.code=500){
          var arry = res.data.res          
          that.setData({
            list: arry
          })
        }else if(res.data.code==200){
          that.setData({
            list: []
          })   
        }
      }
    })
  },

  // 抖宝分类商品
  fl: function (e) {
    var that = this
    
    var c_id=that.data.c_id
    app.util.request({
      url: 'entry/wxapp/Dbcateid',
      data:{
        c_id:c_id
      },
      success: function (res) {
        
        if(res.data.code==200){
          var arry = res.data.res
          that.setData({
            list: arry
          })
        }else if(res.data.code==500){
          that.setData({
            list: []
          })
        }
      }
    })
  },

  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  onPullDownRefresh: function () {
    var that=this
    var menu = that.data.menu
    var activeIndex=that.data.activeIndex
    if (activeIndex==0){
      that.tuijian()      
    }else{
      activeIndex = activeIndex-1
      var c_id = menu[activeIndex].id
      app.util.request({
        url: 'entry/wxapp/Dbcateid',
        data: {
          c_id: c_id
        },
        success: function (res) {
          
          var arry = res.data.res
          that.setData({
            list: arry
          })
        }
      })
    }
    
    wx.stopPullDownRefresh()
},

 /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this
    var activeIndex = that.data.activeIndex
    if (activeIndex==0){
      that.tuijian()      
    } else{
      that.fl()
    }
  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
    // wx.showLoading({
    //   title: '玩命加载中',
    // })
    var that = this
    that.data.page = that.data.page + 1
    
    var i = that.data.activeIndex
		var openid = wx.getStorageSync('openid')
    if (i == 0) {
      app.util.request({
        'url': 'entry/wxapp/dbupfee',
        'cachetime': '0',
        data: { page: that.data.page },
        success: function (res) {
          
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          } else if (res.data.code == 200) {
            for (var i = 0; i < res.data.res.length; i++) {
              that.data.list.push(res.data.res[i])
            }
            that.setData({
              list: that.data.list
            })
            
          }
        }
      })
    } else{
      var that = this
      
      var c_id = that.data.c_id
      // app.util.request({
      //   url: 'entry/wxapp/Dbcateid',
      //   data: {
      //     c_id: c_id
      //   },
      //   success: function (res) {
      //     
      //     var arry = res.data.res
      //     that.setData({
      //       list: arry
      //     })
      //   }
      // })

      app.util.request({
        'url': 'entry/wxapp/Dbcateid',
        'cachetime': '0',
        data: { c_id: c_id, page: that.data.page, state: 1 },
        success: function (res) {
          
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          } else if (res.data.code == 200) {

           
            for (var i = 0;i < res.data.res.length; i++) {
              that.data.list.push(res.data.res[i])
            }
            that.setData({
              list: that.data.list
            })
            
          }
        }
      })
    }
  },


})
/**
   * 页面相关事件处理函数--监听用户下拉动作
   */


  


